<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of DescriptionDaoImpl
 *
 * @author krisada.thiangtham
 */
require '../com.swiss.mooh.dao/DescriptionDao.php';
require '../com.swiss.mooh.utils/ArrayList.php';

class DescriptionDaoImpl implements DescriptionDao {

    public function createNewDescription($productID, $productName, $productDescription, $createDateTime, $menuID, $pageType) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("INSERT INTO SWISS_PAGE_DESCRIPTION VALUES(?,?,?,?,?,?)");
        $stmt->bind_param('ssssss', $productID, $productName, $productDescription, $createDateTime, $menuID, $pageType);
        if ($stmt->execute()) {
            $stmt2 = $mysqli->prepare("SELECT * FROM SWISS_PAGE_DESCRIPTION WHERE CREATED_DATE_TIME = "
                    . "(SELECT MAX(CREATED_DATE_TIME) FROM SWISS_PAGE_DESCRIPTION);");
            $stmt2->execute();
            $meta = $stmt2->result_metadata();
            while ($field = $meta->fetch_field()) {
                $parameters[] = &$row[$field->name];
            }
            call_user_func_array(array($stmt2, 'bind_result'), $parameters);
            while ($stmt2->fetch()) {
                foreach ($row as $key => $val) {
                    $x[$key] = $val;
                }
                return $x['DESC_ID'];
            }
            $stmt2->close();
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function deleteDescriptionByID($descID) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("DELETE FROM SWISS_PAGE_DESCRIPTION WHERE DESC_ID LIKE ?");
        $stmt->bind_param('s', $descID);
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function getAllDescriptionByMenuID($menuID) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $detailList = new ArrayList();
        $stmt = $mysqli->prepare("SELECT * FROM SWISS_PAGE_DESCRIPTION WHERE MENU_ID LIKE ?");
        $stmt->bind_param('s', $menuID);
        $stmt->execute();
        $meta = $stmt->result_metadata();
        while ($field = $meta->fetch_field()) {
            $parameters[] = &$row[$field->name];
        }
        call_user_func_array(array($stmt, 'bind_result'), $parameters);
        while ($stmt->fetch()) {
            foreach ($row as $key => $val) {
                $x[$key] = $val;
            }
            $menuDescp = new Description();
            $menuDescp->setDescDetail($x['DESC_DESC']);
            $menuDescp->setDescID($x['DESC_ID']);
            $menuDescp->setDescName($x['DESC_NAME']);
            $menuDescp->setDescCreatedDateTime($x['CREATED_DATE_TIME']);
            $menuDescp->setMenuID($x['MENU_ID']);
            $menuDescp->setPageType($x['PAGE_TYPE']);
            $detailList->Add($menuDescp);
        }
        $stmt->close();
        return $detailList;
    }

    public function updateDescription($descID, $descName, $productDescription, $pageType) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("UPDATE SWISS_PAGE_DESCRIPTION SET DESC_NAME = ?, DESC_DESC = ?, PAGE_TYPE = ? WHERE DESC_ID LIKE ?");
        $stmt->bind_param('ssss', $descName, $productDescription, $pageType, $descID);
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function getProductDescriptionByID($descID) {
        require '../com.swiss.mooh.model/Description.php';
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("SELECT * FROM SWISS_PAGE_DESCRIPTION WHERE DESC_ID LIKE ?");
        $stmt->bind_param('s', $descID);
        $stmt->execute();
        $meta = $stmt->result_metadata();
        while ($field = $meta->fetch_field()) {
            $parameters[] = &$row[$field->name];
        }
        $menuDescp = new Description();
        call_user_func_array(array($stmt, 'bind_result'), $parameters);
        while ($stmt->fetch()) {
            foreach ($row as $key => $val) {
                $x[$key] = $val;
            }
            $menuDescp->setDescDetail($x['DESC_DESC']);
            $menuDescp->setDescID($x['DESC_ID']);
            $menuDescp->setDescName($x['DESC_NAME']);
            $menuDescp->setDescCreatedDateTime($x['CREATED_DATE_TIME']);
            $menuDescp->setMenuID($x['MENU_ID']);
            $menuDescp->setPageType($x['PAGE_TYPE']);
        }
        $stmt->close();
        return $menuDescp;
    }

}
